package com.weixin.weixinmanage.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.weixin.weixinmanage.entity.News;
import com.weixin.weixinmanage.service.NewsService;

public class NewsAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7731508585889416324L;
	
	private Logger log = Logger.getLogger(NewsAction.class);
	
	private NewsService newsService;
	
	private HashMap<String, Object> queryJson;
	
	private News news;
	
	
	public String getAllData(){
		queryJson = new HashMap<String, Object>();
		try{
			if(news!=null){
				news.setNid(null);
			}
			List<News> newsList = newsService.select(news);
			queryJson.put("result", newsList);
		}catch(Exception e){
			log.error("", e);
		}
		return "getAllData";
	}
	
	public String toUpdate(){
		try{
			news = newsService.selectById(news.getNid());
		}catch(Exception e){
			log.error("", e);
		}
		return "toUpdate";
	}
	
	public String add() throws IOException{
		try{
			newsService.add(news);
		}catch(Exception e){
			log.error("", e);
		}
		String html = "<script language='javascript'>parent.window.closeOver();</script>";
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setContentType("text/html;charset=utf-8"); 
	    response.setCharacterEncoding("UTF-8");
	    response.setHeader("Cache-Control", "no-cache"); 
	    PrintWriter out = response.getWriter(); 
		out.println(html);
	    out.flush();
	    out.close();
	    news.setNid(null);
		return null;
	}
	
	public String update() throws IOException{
		try{
			newsService.update(news);
		}catch(Exception e){
			log.error("", e);
		}
		String html = "<script language='javascript'>parent.window.closeOver();</script>";
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setContentType("text/html;charset=utf-8"); 
	    response.setCharacterEncoding("UTF-8");
	    response.setHeader("Cache-Control", "no-cache"); 
	    PrintWriter out = response.getWriter(); 
		out.println(html);
	    out.flush();
	    out.close();
	    news.setNid(null);
		return null;
	}
	
	public String del(){
		queryJson = new HashMap<String, Object>();
		try{
			newsService.update(news);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		news.setNid(null);
		return "del";
	}
	
	public String publish(){
		queryJson = new HashMap<String, Object>();
		try{
			newsService.update(news);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		news.setNid(null);
		return "publish";
	}
	
	public String regain(){
		queryJson = new HashMap<String, Object>();
		try{
			newsService.update(news);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		news.setNid(null);
		return "regain";
	}
	
	
	public String huishou(){
		queryJson = new HashMap<String, Object>();
		try{
			newsService.update(news);
			queryJson.put("message", "success");
		}catch(Exception e){
			queryJson.put("message", "fail");
		}
		news.setNid(null);
		return "huishou";
	}
	

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}


	public HashMap<String, Object> getQueryJson() {
		return queryJson;
	}


	public void setQueryJson(HashMap<String, Object> queryJson) {
		this.queryJson = queryJson;
	}


	public News getNews() {
		return news;
	}


	public void setNews(News news) {
		this.news = news;
	}

}
